<?php

/**
 * This is the model class for table "sd.goods_view".
 *
 * The followings are the available columns in table 'sd.goods_view':
 * @property string $id
 * @property string $goods_id
 * @property string $before_view_id
 * @property string $time
 * @property string $ip
 *
 * The followings are the available model relations:
 * @property Goods $beforeView
 * @property Goods $goods
 */
class GoodsView extends CBaseActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.goods_view';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('goods_id, time', 'required'),
			array('goods_id, before_view_id, time', 'length', 'max'=>10),
			array('ip', 'length', 'max'=>15),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, goods_id, before_view_id, time, ip', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'beforeView' => array(self::BELONGS_TO, 'Goods', 'before_view_id'),
			'goods' => array(self::BELONGS_TO, 'Goods', 'goods_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'goods_id' => 'Goods',
			'before_view_id' => 'Before View',
			'time' => 'Time',
			'ip' => 'Ip',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('goods_id',$this->goods_id,true);
		$criteria->compare('before_view_id',$this->before_view_id,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('ip',$this->ip,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return GoodsView the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function getViewedGoodsId($id, $limit = 5)
	{
//		$search = new SphinxClient();
//		$search->ResetGroupBy();
//		$search->ResetFilters();
//		$search->SetArrayResult(false);
//		$search->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
//		$search->SetMatchMode(SPH_MATCH_EXTENDED);
//		$search->SetFilter('before_view_id', array($id));
//
//		$res = $search->Query('', 'goods_view');
//
//		if(empty($res['matches']))
//			return array();
//
//		return array_keys($res['matches']);
//		print_r($res); exit;

		$oRowset = $this->findAllBySql("
			SELECT gv2.* FROM `goods_view` gv1
			INNER JOIN goods_view gv2
			ON gv2.goods_id = gv1.before_view_id OR gv2.before_view_id = gv1.goods_id
			WHERE gv1.goods_id IN(". implode(', ', (array)$id) .") OR gv1.before_view_id IN(". implode(', ', (array)$id) .")
			GROUP BY gv2.before_view_id
			LIMIT ". $limit ."
		");

		$aIds = array();
		foreach($oRowset as $oRow)
		{
			$aIds[$oRow->goods_id] = $oRow->goods_id;
			$aIds[$oRow->before_view_id] = $oRow->before_view_id;
		}

		return $aIds;
	}

	public function getTopGoodsId($limit = 10)
	{
		$oCriteria = $this->getDbCriteria();
		$oCriteria->select = 'goods_id';
		$oCriteria->group = 'goods_id';
		$oCriteria->addCondition(array('time > '.(time()-2592000)));
		$oCriteria->limit = $limit;

		$oRowset = $this->findAll($oCriteria);

		$aIds = array();
		foreach($oRowset as $oRow)
		{
			$aIds[$oRow->goods_id] = $oRow->goods_id;
		}

		return $aIds;
	}

}
